package ncsutest.util;
import com.sun.spot.resources.transducers.ITriColorLED;
import com.sun.spot.resources.transducers.LEDColor;
import com.sun.spot.sensorboard.EDemoBoard;

public class LEDs {
	private static ITriColorLED[] leds = EDemoBoard.getInstance().getLEDs();

	public static void displayBinaryNumber(LEDColor color, int x, int baseLED) {
		for (int i = baseLED; i >= 0; i--) {
			leds[i].setColor(color);
			leds[i].setOn((x & (1 << (baseLED - i))) != 0);
		}
	}
	
    public static void clearLEDs() {
    	for (int i = 0; i < leds.length; i++) leds[i].setOff();
    }
    
    public static void setLED(int index, LEDColor color) {
    	leds[index].setColor(color);
    	leds[index].setOn();
    }

    public static void setLEDs(int low, int high, LEDColor color) {
    	for (int i = low; i <= high; i++) {
    		leds[i].setColor(color);
        	leds[i].setOn();
    	}
    }

    public static void clearLED(int index) {
    	leds[index].setOff();
    }

    public static void clearLEDs(int low, int high) {
    	for (int i = low; i <= high; i++) leds[i].setOff();
    }

    public static void progessBar(int base, int length, float percent, int r, int g, int b) {
    	float bar = percent * length;
    	for (int i = 0; i < length; i++) {
    		if (i+1 <= bar) {
    			leds[base+i].setRGB(r, g, b);
    			leds[base+i].setOn();
    		} else if (i <= bar) {
    			float p = bar - i;
    			leds[base+i].setRGB((int)(r*p), (int)(g*p), (int)(b*p));
    			leds[base+i].setOn();
    		} else break;
    	}
    }
}
